from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import Optional

mcp = FastMCP("GNOME Shell MCP Server")

"""
面向软件：gnome-shell

MCP tools 列表：
- gnome_shell_list_modes: 列出 gnome-shell 支持的模式
- gnome_shell_version: 获取 gnome-shell 版本信息
- gnome_shell_help: 显示 gnome-shell 帮助信息
- gnome_shell_help_all: 显示 gnome-shell 完整帮助信息
- gnome_shell_replace: 替换当前 gnome-shell 会话
- gnome_shell_perf_tool_version: 获取 gnome-shell-perf-tool 版本信息
- gnome_shell_perf_tool_help: 显示 gnome-shell-perf-tool 帮助信息
- gnome_shell_extension_tool_help: 显示 gnome-shell-extension-tool 帮助信息

所有工具返回统一 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

@mcp.tool()
def gnome_shell_list_modes() -> dict:
    """
    列出 gnome-shell 支持的所有模式
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell", "--list-modes"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_version() -> dict:
    """
    获取 gnome-shell 版本信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell", "--version"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_help() -> dict:
    """
    显示 gnome-shell 基本帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell", "-h"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_help_all() -> dict:
    """
    显示 gnome-shell 完整帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell", "--help-all"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_replace(timeout_seconds: Optional[int] = 5) -> dict:
    """
    替换当前 gnome-shell 会话
    
    参数:
        timeout_seconds: 超时时间（秒），默认5秒
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["timeout", f"{timeout_seconds}s", "gnome-shell", "--replace"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_perf_tool_version() -> dict:
    """
    获取 gnome-shell-perf-tool 版本信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell-perf-tool", "--version"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_perf_tool_help() -> dict:
    """
    显示 gnome-shell-perf-tool 帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell-perf-tool", "-h"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def gnome_shell_extension_tool_help() -> dict:
    """
    显示 gnome-shell-extension-tool 帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    cmd = ["gnome-shell-extension-tool", "-h"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(cmd),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()